using System;
using System.IO;
using System.Text;
namespace ICSharpCode.TextEditor.Util
{
	public static class FileReader
	{
		public static bool IsUnicode(Encoding encoding)
		{
			int codePage = encoding.CodePage;
			return codePage == 65001 || codePage == 65000 || codePage == 1200 || codePage == 1201;
		}
		public static string ReadFileContent(Stream fs, ref Encoding encoding)
		{
			string result;
			using (StreamReader streamReader = FileReader.OpenStream(fs, encoding))
			{
				streamReader.Peek();
				encoding = streamReader.CurrentEncoding;
				result = streamReader.ReadToEnd();
			}
			return result;
		}
		public static string ReadFileContent(string fileName, Encoding encoding)
		{
			string result;
			using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			{
				result = FileReader.ReadFileContent(fileStream, ref encoding);
			}
			return result;
		}
		public static StreamReader OpenStream(Stream fs, Encoding defaultEncoding)
		{
			if (fs == null)
			{
				throw new ArgumentNullException("fs");
			}
			if (fs.Length >= 2L)
			{
				int num = fs.ReadByte();
				int num2 = fs.ReadByte();
				int num3 = num << 8 | num2;
				if (num3 <= 61371)
				{
					if (num3 != 0 && num3 != 61371)
					{
						goto IL_61;
					}
				}
				else
				{
					if (num3 != 65279 && num3 != 65534)
					{
						goto IL_61;
					}
				}
				fs.Position = 0L;
				return new StreamReader(fs);
				IL_61:
				return FileReader.AutoDetect(fs, (byte)num, (byte)num2, defaultEncoding);
			}
			if (defaultEncoding != null)
			{
				return new StreamReader(fs, defaultEncoding);
			}
			return new StreamReader(fs);
		}
		private static StreamReader AutoDetect(Stream fs, byte firstByte, byte secondByte, Encoding defaultEncoding)
		{
			int num = (int)Math.Min(fs.Length, 500000L);
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < num; i++)
			{
				byte b;
				if (i == 0)
				{
					b = firstByte;
				}
				else
				{
					if (i == 1)
					{
						b = secondByte;
					}
					else
					{
						b = (byte)fs.ReadByte();
					}
				}
				if (b < 128)
				{
					if (num2 == 3)
					{
						num2 = 1;
						break;
					}
				}
				else
				{
					if (b < 192)
					{
						if (num2 != 3)
						{
							num2 = 1;
							break;
						}
						num3--;
						if (num3 < 0)
						{
							num2 = 1;
							break;
						}
						if (num3 == 0)
						{
							num2 = 2;
						}
					}
					else
					{
						if (b < 194 || b >= 245)
						{
							num2 = 1;
							break;
						}
						if (num2 != 2 && num2 != 0)
						{
							num2 = 1;
							break;
						}
						num2 = 3;
						if (b < 224)
						{
							num3 = 1;
						}
						else
						{
							if (b < 240)
							{
								num3 = 2;
							}
							else
							{
								num3 = 3;
							}
						}
					}
				}
			}
			fs.Position = 0L;
			switch (num2)
			{
				case 0:
				case 1:
				{
					if (FileReader.IsUnicode(defaultEncoding))
					{
						defaultEncoding = Encoding.Default;
					}
					return new StreamReader(fs, defaultEncoding);
				}
				default:
				{
					return new StreamReader(fs);
				}
			}
		}
	}
}
